package news.yp.newsproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import news.yp.newsproject.R;
import news.yp.newsproject.bean.BookResponse;
import news.yp.newsproject.listener.OnItemClickListener;
import news.yp.newsproject.network.ApiServiceFactor;
import news.yp.newsproject.utils.Utils;
import news.yp.newsproject.view.GlideRoundTransform;

/**
 * desc: 看书的recyleView Adapter
 * date:18-10-30
 * who:yp666
 */
public class BookRecyleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<BookResponse.DataBean> booklists;
    private boolean chanage = false;

    public BookRecyleAdapter(Context context, List<BookResponse.DataBean> booklists) {
        this.mContext = context;
        this.booklists = booklists;
    }

    public void refrsh(List<BookResponse.DataBean> booklists) {
        this.booklists.clear();
        this.booklists.addAll(booklists);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.bookrecyle_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        Glide.with(mContext).load(ApiServiceFactor.bookurl + "getimg/" + booklists.get(position).getBookjpg())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.book_loading)
                .error(R.mipmap.book_pic_error).transform(new GlideRoundTransform(mContext, 30))
                .into(((ViewHolder) viewHolder).mbookImg);
        String[] split;
        String text;
        try {
            split = booklists.get(position).getBookpath().split("&");
            text = split[split.length - 1];
        } catch (Exception e) {
            text = "";
        }
        ((ViewHolder) viewHolder).mbookName.setText(text);
        if (chanage) {
            ((ViewHolder) viewHolder).mbookImg.getLayoutParams().height = Utils.dip2px(180);
        } else {
            ((ViewHolder) viewHolder).mbookImg.getLayoutParams().height = Utils.dip2px(220);
        }
        chanage = !chanage;
    }

    @Override
    public int getItemCount() {
        return booklists.size();
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mbookImg;
        private TextView mbookName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mbookImg = itemView.findViewById(R.id.book_img);
            mbookName = itemView.findViewById(R.id.book_name);
            mbookImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemOnClick(v, getAdapterPosition());
                }
            });
        }
    }
}
